<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>
    PHP plant |
    
    无敌小笼包</title>
  
    <link rel="shortcut icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
  
  <script src="/js/pace.min.js"></script>
</head>

<body>
<main class="content">
  <section class="outer">
  <article id="post-php工厂模式" class="article article-type-post" itemscope itemprop="blogPost">

  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      PHP plant
    </h1>
  

      </header>
    

    
      <div class="article-meta">
        <a href="/2016/05/07/php工厂模式/" class="article-date">
  <time datetime="2016-05-07T02:20:12.000Z" itemprop="datePublished">2016-05-07</time>
</a>
        
  <div class="article-category">
    <a class="article-category-link" href="/categories/Skill/">Skill</a>
  </div>

      </div>
    

    <div class="article-entry" itemprop="articleBody">
      
      
      
        <p> 顾名思义，工厂是可以加工零件的，PHP程序中的工厂模式也有相同的功能，可以方便的使用一个静态的工厂方法来实例化某一个类，那么这样做的好处是什么呢？初学PHP的设计模式，实例化一个类会给它一些参数以便在其构析的时候可以根据不同的参数反馈出我们需要的结果。<br><a id="more"></a></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div></pre></td><td class="code"><pre><div class="line">举例说明，以下是一个User类，非常简单：</div><div class="line"><span class="comment">/**</span></div><div class="line">*     factory pattern 工厂模式</div><div class="line">*/</div><div class="line">    <span class="class"><span class="keyword">interface</span> <span class="title">abstracted</span>        </span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">realCreate</span><span class="params">()</span></span>;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//女人类</span></div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Woman</span></span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">action</span><span class="params">()</span></span></div><div class="line">        &#123;</div><div class="line">            <span class="keyword">echo</span> <span class="string">'这是女人'</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//男人类</span></div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Man</span></span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">action</span><span class="params">()</span></span></div><div class="line">        &#123;</div><div class="line">            <span class="keyword">echo</span> <span class="string">'这是男人'</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//创建女人</span></div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">WomanCreator</span> <span class="keyword">implements</span> <span class="title">abstracted</span> </span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">public</span> $chromosome;<span class="comment">//染色体</span></div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">realCreate</span><span class="params">()</span></span>&#123;</div><div class="line">            <span class="keyword">if</span> (<span class="keyword">$this</span>-&gt;chromosome == <span class="string">"xx"</span>) </div><div class="line">            &#123;</div><div class="line">                <span class="keyword">return</span> <span class="keyword">new</span> Woman();</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//创建男人</span></div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">ManCreator</span> <span class="keyword">implements</span> <span class="title">abstracted</span> </span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">public</span> $chromosome;</div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">realCreate</span><span class="params">()</span></span>&#123;</div><div class="line">            <span class="keyword">if</span> (<span class="keyword">$this</span>-&gt;chromosome == <span class="string">"xy"</span> || <span class="keyword">$this</span>-&gt;chromosome == <span class="string">"xyy"</span>) </div><div class="line">            &#123;</div><div class="line">                <span class="keyword">return</span> <span class="keyword">new</span> Man();</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//人类工厂</span></div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">PersonFactory</span></span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">create</span><span class="params">($what)</span></span></div><div class="line">        &#123;</div><div class="line">            $create = $what.<span class="string">"Creator"</span>;  <span class="comment">//womanCreator</span></div><div class="line">            <span class="keyword">return</span>  <span class="keyword">new</span> $create();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    $create = <span class="keyword">new</span> PersonFactory();</div><div class="line">    $instance = $create-&gt;create(<span class="string">'woman'</span>);</div><div class="line">    $instance-&gt;chromosome = <span class="string">"xx"</span>;</div><div class="line">    $instance-&gt;realCreate()-&gt;action();</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://blog.mango16.cc/2016/05/07/php工厂模式/" data-id="cjrbtw1yr001w6d8hr5wlsmhl"
         class="article-share-link">分享</a>
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Design-Pattern/">Design Pattern</a></li></ul>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
      <a href="/2016/05/10/phpsingerl/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            PHP 单例模式
          
        </div>
      </a>
    
    
      <a href="/2016/05/05/php画图形/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">PHP 画图形 N行N列</div>
      </a>
    
  </nav>


  

  
    
  <div class="gitalk" id="gitalk-container"></div>
  <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
  <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
  <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
  <script type="text/javascript">
      var gitalk = new Gitalk({
        clientID: 'caba8826a61790936307',
        clientSecret: '09a15037dcab30d9adf35a53ec6961939169f797',
        repo: 'm9rco.github.io',
        owner: 'm9rco',
        admin: ['m9rco'],
        // id: location.pathname,      // Ensure uniqueness and length less than 50
        id: md5(location.pathname),
        distractionFreeMode: false  // Facebook-like distraction free mode
      })

  gitalk.render('gitalk-container')
  </script>

  

</article>



</section>
  <footer class="footer">
  
  <div class="outer">
    <ul class="list-inline">
      <li>&copy; 2019 无敌小笼包</li>
      <i class="pulse"></i>
    </ul>
  </div>
</footer>

</main>
<aside class="sidebar">
  <button class="navbar-toggle"></button>

<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/hexo.svg" alt="无敌小笼包"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">归档</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/gallery">相册</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/nagging">叽咕</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="Search">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>

<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>

<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
</aside>
  <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>


  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/search.js"></script>


<script src="/js/ocean.js"></script>

</body>
</html>